<?php

require 'data/data.php';
session_start();

$isLogin = (isset($_SESSION['user']) && !empty($_SESSION['user'])) ? true : false;
$user = isset($_SESSION['user']) ? $_SESSION['user'] : ['id' => '', 'user_name' => '', 'password' => ''];

//session方式
if (isset($_SESSION['cart'])) $cart = $_SESSION['cart'];

$sql = 'select * from `goods`';
$goodsData = query($sql);

//数据库方式
$cartInfo = query('select * from `cart` where user_name = ?', [$user['user_name']]);
// print_r($cartInfo); exit;
?>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>购物车</title>
		<link rel="stylesheet" href="public/css/shopcar.css" />
	</head>
	<body>
		<div class="header">
			<img src="public/img/vancl-logo.png" alt="vancl" />
			<!--未登录-->
			<?php if (!$isLogin): ?>
			<p class="userInfo">
				您好,欢迎光临凡客诚品！
				<a href="login.php">登录</a>
				<span class="spacer">|</span>
				<a href="register.php">注册</a>
			</p>
			<!--未登录-->
			<!--已登录-->
			<?php else: ?>
			<p class="userInfo">
				您好,<span class="username"><?= $user['user_name'] ?></span>！
				<a href="action/logout_action.php">退出登录</a>
				<span class="spacer">|</span>
				<a href="login.php">更换用户</a>
			</p>
			<?php endif ?>
			<!--已登录-->
			<a href="">帮助中心</a>
		</div>
		<div class="body">
			<p>我的购物车</p>
			<span class="warning"><span class="c_red">温馨提示：</span>1.选购清单中的商品无法保留库存，请您及时结算。2.商品的价格和库存将以订单提交时为准。3.促销活动满减优惠将均摊至商品小计中。</span>
		</div>
		
		
		<!--没有商品-->
			<?php if (count($cartInfo) == 0): ?>
			<div class="empty">
				您的购物车中没有商品，请您先 <a href="index.php">选购商品»</a>
			</div>
		<!--没有商品-->
			
			
		<!--有商品-->
		<?php elseif (count($cartInfo) > 0): ?>
		<div class="contents">
			<ul>
				<li><input type="checkbox" name="selectAll" id="selectAll" /><label for="selectAll">全选</label></li>
				<li class="ml_200">商品信息</li>
				<li class="ml_200">单价</li>
				<li class="ml_80">数量</li>
				<li class="ml_80">小计</li>
				<li class="ml_80">操作</li>
			</ul>			
			<?php foreach ($cartInfo as $cartGoods): ?>		
			<div class="content">
				<input type="checkbox" class="sel"/>
				<img src="<?= $cartGoods['image'] ?>" alt=""/>
				<span class="gname"><?= $cartGoods['name'] ?></span>
				<span class="price">¥<?= $cartGoods['price'] ?></span>
				<span class="num"><a href="action/change_quantity.php?gid=<?= $cartGoods['gid'] ?>&op=1">-</a><input type="text" value="<?= $cartGoods['num'] ?>" class="number"/><a href="action/change_quantity.php?gid=<?= $cartGoods['gid'] ?>&op=2">+</a></span>
				<span class="count">¥<?= $cartGoods['num'] * $cartGoods['price'] ?></span>
				<a href="action/delete_commodity.php?gid=<?= $cartGoods['gid'] ?>" class="del">删除</a>
			</div>
			<?php endforeach; ?>
			<?php endif; ?>
		</div>
		<div class="pay">
			<p><a class="delSel">删除选中</a><span class="totalNum">数量总计:0件</span></p>
			<p>产品金额总计(不含运费): <span class="totalMoney">¥0.00</span><a href="index.php">继续购物</a><a href="">去结算</a></p> 
		</div>
		<!--有商品-->
		
		
		<div class="tail">
			<p>Copyright 2007 - 2017 vancl.com All Rights Reserved 京ICP证100557号 京公网安备11010102000579号 出版物经营许可证新出发京批字第直110138号</p>
		</div>
	</body>
</html>
